/*
 * $Id: CommandPool.java 89 2012-12-28 10:57:30Z Denis.Skalsky@gmail.com $
 */
package com.globallogic.skalskyy.javabasic.valstaff.commands;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import com.globallogic.skalskyy.javabasic.valstaff.Employee;
import com.globallogic.skalskyy.javabasic.valstaff.StaffList;


/**
 * CommandPool is a entry point for commands processing.
 * @author $Author: Denis.Skalsky@gmail.com $
 * @version $Revision: 89 $
 */
public class CommandPool {
	
	private static final String COMMAND_FINDING = "find";
	private static final String COMMAND_EMPLOYEE_BY_FIRSTNAME = "firstname";
	private static final String COMMAND_EMPLOYEE_BY_SECONDNAME = "secondname";	
	private static final String COMMAND_EMPLOYEE_BY_POSITION = "position";
	private static final String COMMAND_EMPLOYEE_BY_DATE_OF_BIRTH = "dateofbirth";	
	private static final String COMMAND_EMPLOYEE_BY_LANGUAGE = "language";
	private static final String COMMAND_EMPLOYEE_BY_TESTER_TYPE = "testertype";
	
	private static final String COMMAND_GETTER = "get";
	private static final String COMMAND_SOFTWARE_ENGINEER_LIST = "softwareengineerlist";
	private static final String COMMAND_TEST_ENGINEER_LIST = "testengineerlist";
	private static final String COMMAND_STAFF_LIST = "stafflist";
	private static final String COMMAND_COUNT_OF_EMPLOYEE = "countofemployee";
	
	private static final String COMMAND_HELP = "help";
	
	private static final String COMMAND_SEPARATOR = " ";
	
	private static CommandPool instance = null;
	
	private HashMap<String, Command> commands = new HashMap<String, Command>();

	private CommandPool() {
		commands.put(COMMAND_FINDING, new Finding());
		commands.put(COMMAND_EMPLOYEE_BY_FIRSTNAME, new EmployeeByFirstName());
		commands.put(COMMAND_EMPLOYEE_BY_SECONDNAME, new EmployeeBySecondName());
		commands.put(COMMAND_EMPLOYEE_BY_POSITION, new EmployeeByPosition());
		commands.put(COMMAND_EMPLOYEE_BY_DATE_OF_BIRTH, new EmployeeByDateOfBirth());
		commands.put(COMMAND_EMPLOYEE_BY_LANGUAGE, new EmployeeByLanguage());
		commands.put(COMMAND_EMPLOYEE_BY_TESTER_TYPE, new EmployeeByTesterType());
		
		commands.put(COMMAND_GETTER, new Getter());
		commands.put(COMMAND_SOFTWARE_ENGINEER_LIST, new SoftwareEngineerList());
		commands.put(COMMAND_TEST_ENGINEER_LIST, new TestEngineerList());
		commands.put(COMMAND_STAFF_LIST, new StaffList());
		commands.put(COMMAND_COUNT_OF_EMPLOYEE, new CountOfEmployee());
		
		commands.put(COMMAND_HELP, new Help());
	}
	
	/** Get Command object by command name
	 * @param name command's name
	 * @return Command if command isn't found then will be returned object of Help command
	 */
	protected Command getCommand(String name) {
		Command command = commands.get(name);
		if (command == null) {
			return commands.get(CommandPool.COMMAND_HELP);
		} else {
			return command;
		}
	}
	
	public static CommandPool getInstance() {
		if (instance == null) {
			instance = new CommandPool();
		}
		
		return instance;
	}
	
	public void executeCommand(String commandLine, OutputResult outputResult) {
		StringTokenizer args = new StringTokenizer(commandLine.toLowerCase(), COMMAND_SEPARATOR);		
		Command command = CommandPool.getInstance().getCommand(args.nextToken());			
		List<String> result = command.execute((args.hasMoreTokens() ? args.nextToken(Command.EMPTY_STRING).trim() : Command.EMPTY_STRING));		
		Collections.sort(result);
		outputResult.print(result);
	}
	
	public List<String> convertEmployeesToString(List<Employee> employees) {
		List<String> result = new ArrayList<String>();
		
		for (Employee employee : employees) {
			result.add(employee.toString());
		}
		
		return result;
	}
}